home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / DIALOG.ZIP / DBPRO2-@.EXE / SIMPLEDB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-20  |  20.2 KB  |  614 lines

  1. /*
  2. A simple dialog box example -- a fill in the blank and a multiple choice
  3. question with two buttons (Confirm and Abort.)
  4. */
  5.  
  6. #include "pro.h"
  7. #include "xglobals.h"
  8. #include "dbpro.h"
  9. #include "colors.h"
  10. #include "xcodes.h"
  11. #include "video.h"
  12. #include <string.h>
  13. #include <conio.h>
  14.  
  15. void main(void);
  16. int blip(void *);
  17.  
  18. /* target variables -- target variables can be linked to a question and are
  19. updated when the dialog box is exited.  DO NOT free() target variables for
  20. free_form's, the pointer to the current response is simply assigned to
  21. the target variable.  When the dialog box is rerun it will free the last
  22. response and substitute it with the current response.  If you have already
  23. freed the same memory block -- well you know.
  24. */
  25.  
  26. char *name;                    /* first question target variable */
  27. int job;                    /* second question target variable */
  28.  
  29. /* miscellaneous data for the dialog box */
  30. char *responses[] = {        /* possible responses to the multiple choice */
  31.     "Doctor",                /* question */
  32.     "Lawyer",
  33.     "Butcher",
  34.     "Welder",
  35.     "Accountant",
  36.     "Housewife",
  37.     "Mechanic",
  38.     "Pilot",
  39.     NULL
  40. };
  41.  
  42. char available[] = {        /* indicates types of responses in multiple */
  43.     1,                        /* choice question, 0 - not available, */
  44.     1,                        /* 1 - available, 2 - title (not available) */
  45.     1,
  46.     1,
  47.     1,
  48.     1,
  49.     1,
  50.     1,
  51.     1,
  52.     1
  53. };
  54.  
  55. char db_available[] = {        /* the dialog box consists of four questions */
  56.     1,                        /* this array indicates which questions can */
  57.     1,                        /* be modified, 1-yes, 0-no */
  58.     1,
  59.     1,1,1,1,1,1,1,1
  60. };
  61.  
  62. char *exit_codes[] = {        /* corresponds to the possible exit values */
  63.     "ABORT",                /* generated by a dialog box */
  64.     "CONFIRM",
  65.     "ASCEND",
  66.     "DESCEND",
  67.     "MOUSE",
  68.     "","","","","","",
  69.     "MENU"};
  70.  
  71. char *db_help[] = {            /* dialog box help screen text */
  72.     " ",
  73.     " Press ENTER to confirm.",
  74.     " Press ESC to abort.",
  75.     " Press arrow keys to choose a question.",
  76.     " Press alpha to change text questions.",
  77.     " Press spacebar or grey+ to change multiple choice questions. ",
  78.     " ",
  79.     " Press any key to continue ",
  80.     "",
  81.     NULL
  82. };
  83.  
  84. /* help routine -- this function displays the dialog box help text */
  85. int help(db_ptr)
  86. dialog_box *db_ptr;
  87. {
  88.     unsigned handle;
  89.  
  90.     db_ptr = db_ptr;
  91.     scroll_bars_on = NONE;
  92.     wn_openw(handle = wn_putslist(db_help,white,black,wherex(),wherey(),
  93.         HEAD_ON,NULL,NULL));
  94.     getch();
  95.     wn_delw(handle);
  96.     return(TRUE);
  97. }
  98.  
  99. /*
  100.     links the F1 key to the help() function (creating a macro
  101.     called context_help)
  102. */
  103. makemacro(context_help,XF1,help)
  104.  
  105. /* a null terminated list of macros used by this dialog box */
  106. unsigned *allmacros[] = {
  107.     context_help,
  108.     NULL
  109. };
  110.  
  111. /* definition of each the question used in the dialog box */
  112.  
  113. free_form first_question = {
  114.     FREE_FORM,              /* question type */
  115.     0,                        /* tile handle -- if your dialog box consists
  116.                                of several virtual screens you can indicate
  117.                                which virtual screen the question is
  118.                                displayed in
  119.                             */
  120.     NULL, NULL, NULL,        /* in - this routine will be executed when the
  121.                                question is initially highlighted.
  122.                                out - this routine will be executed when the
  123.                                question is dehighlighted,
  124.                                action - this routine will be executed each
  125.                                time the question is exited (e.g. when you
  126.                                exit the free_form line editor)
  127.                             */
  128.     "~Name",                /* statement */
  129.     2, 2,                    /* statement x, y */
  130.     XALTN,                    /* shortcut key - the actual keypress value
  131.                                which when recognized will navigate directly
  132.                                to this question
  133.                             */
  134.     NULL, "Unknown",        /* response & default response - the response
  135.                                contains the current response to the question
  136.                                typically you would assign this a NULL value,
  137.                                if you do assign it a value make sure that it
  138.                                is a pointer to a dynamically allocated memory
  139.                                block.
  140.  
  141.                                default response - if response is NULL db_run
  142.                                uses a dynamically allocated copy of the
  143.                                default response as the actual response.
  144.                             */
  145.     19, 2, 30,                /* response x, y, and length - indicates where
  146.                                to display the response string and how many
  147.                                characters to devote to the display area
  148.                             */
  149.     NULL,                    /* insurance -- typically set to NULL, this
  150.                                member of the structure is used to keep
  151.                                a copy of the string when it is edited, if
  152.                                the user wants to restore the original
  153.                                string we use this copy to recreate it
  154.                             */
  155.     &name,                    /* target -- this char** is updated to the
  156.                                same value as response on exiting the
  157.                                dialog box.  You can use this to have
  158.                                dialog boxes update global (or local)
  159.                                variables in your existing routines.
  160.                             */
  161.     0,                        /* cursor position - indicates the default
  162.                                position of the edit cursor on editing
  163.                                the response string
  164.                             */
  165.     NULL,                    /* macros - if you want to have some special
  166.                                macros during the line editor process
  167.                                assign them here.
  168.                             */
  169.     TRUE                    /* refresh - always set to TRUE */
  170. };
  171.  
  172. multi_choice second_question = {
  173.  
  174.     /*************************** same as above ****************************/
  175.     MULTI_CHOICE,              /* question type */
  176.     0,                        /* tile handle */
  177.     NULL, NULL, NULL,        /* in, out, action */
  178.     "~Occupation",            /* statement */
  179.     2, 5,                    /* statement x, y */
  180.     XALTO,                    /* shortcut key */
  181.  
  182.     /********** these fields differ from the free_form question ********/
  183.  
  184.     NULL,                    /* response mask - you can elect to not display
  185.                                the statement portion and the response portion
  186.                                and to substitute this string constant in the
  187.                                same position as the response.  This makes a
  188.                                multiple choice question behave like a pull
  189.                                down menu.  To not use this feature set this
  190.                                to NULL.
  191.                             */
  192.     responses,                /* response list - a NULL terminated list of
  193.                                strings representing the possible responses
  194.                                to the multiple choice question.
  195.                             */
  196.     available,                /* available - a list of chars which corresponds
  197.                                to the above responses list, where a 0
  198.                                indicates that the string could be a valid
  199.                                response but is not at the moment.  A 1
  200.                                indicates that it is a valid response, and a
  201.                                2 indicates that the item is never a valid
  202.                                response, but is simply a title used to
  203.                                segment the list
  204.                             */
  205.     0,                        /* default response - indicates the
  206.                                member of the above responses list which
  207.                                is the current response to the question.
  208.                             */
  209.     19, 5, 30,                /* response x, y, and length - same as a
  210.                                free_form
  211.                             */
  212.     MAX_WINDOWS, 0,            /* aux window & tile - a multiple choice
  213.                                question requires a window to display
  214.                                the pull down list in -- set this to
  215.                                MAX_WINDOWS to have db_run create the
  216.                                window as needed.
  217.                             */
  218.     box1,                    /* if window is created (see above) it will
  219.                                use these border characters
  220.                             */
  221.     "Profession",            /* and -- it will have this name at the top */
  222.     NULL,                    /* this name at the bottom */
  223.     HEAD_ON,                /* and will use this shading style */
  224.     NULL,                    /* list pointer - a multiple choice question
  225.                                requires a list_rec for the pull down list.
  226.                                Set this value to NULL and db_run will create
  227.                                the list_rec as needed.
  228.                             */
  229.     &job,                    /* target - the int* will be updated with the
  230.                                current response offset on exiting the
  231.                                dialog box.  Use this field to update
  232.                                global (or local) variables in your
  233.                                application
  234.                             */
  235.     NULL,                    /* macros - If you want to have some special
  236.                                macros active in the pull down lists, assign
  237.                                them to this field.
  238.                             */
  239.     4,                        /* if you want the pull down list to be displayed
  240.                                in side by side columns indicate the number of
  241.                                columns here.  If you want a horizontal list
  242.                                (like a LOTUS menu use MAXINT.)
  243.                             */
  244.     TRUE                    /* refresh - always set to TRUE */
  245. };
  246.  
  247. button confirm_button = {
  248.  
  249.     /************************* same as above *********************/
  250.     BUTTON,                    /* question type */
  251.     0,                        /* tile handle */
  252.     NULL, NULL, NULL,        /* in, out, and action */
  253.     "~Confirm",                /* statement */
  254.     16, 7,                    /* statement x, y */
  255.     XALTC,                    /* shortcut key */
  256.  
  257.     /*************** these fields are unique to buttons ***********/
  258.     box0,                    /* border characters - if you want the
  259.                                button statement surrounded by a box
  260.                                indicate the box type here.  Otherwise
  261.                                set this field to NULL.
  262.                             */
  263.     HEAD_ON,                /* shading - if your button is surrounded
  264.                                by a box you can use a shadowing effect
  265.                                with this field.
  266.                             */
  267.     XENTER,                    /* hitting button is = XENTER - when a button
  268.                                is pressed it simply pushes a key value
  269.                                or db opcode onto the queue.  The
  270.                                interpretation of this value can then
  271.                                do things like confirm the dialog box, etc.
  272.                                If you do not want to use this feature of
  273.                                buttons assign this field a value of
  274.                                DO_NOTHING.
  275.                             */
  276.     XENTER,                    /* flash key - you can 'press' a button without
  277.                                changing the selected question to the button
  278.                                by pressing this key value.  Note that the
  279.                                mouse routines simply put the shortcut key
  280.                                value in the queue when you press and release
  281.                                on a button and they put this key value into
  282.                                the queue when you quickly click on a button.
  283.                                As such this must be a unique value, otherwise
  284.                                clicking on this button, could actually activate
  285.                                another button.  Note that the value does
  286.                                not have to be a keyboard generatable value it
  287.                                should just be unique to the other shortcut keys.
  288.                             */
  289.     FALSE,                    /* continuous - if holding the mouse button down
  290.                                and sitting on a dialog box button should
  291.                                continuously push shortcut key values onto the
  292.                                queue indicate TRUE in this field, otherwise
  293.                                indicate FALSE.  Generally you would indicate
  294.                                TRUE if it made sense for the button to perform
  295.                                some continuous action if you held the mouse
  296.                                cursor on it.  For example, if you had button
  297.                                which 'flipped' through a series of records
  298.                                you might make this field TRUE.
  299.                             */
  300.     CONFIRMED                /* exit value - if the button results in
  301.                                exiting the dialog box you can change the
  302.                                exit value with this field.  You can use
  303.                                this if you want to specifically determine
  304.                                that a user exited by using a certain
  305.                                button.
  306.                             */
  307. };
  308.  
  309. button abort_button = {
  310.  
  311.     /************************* same as above ***************************/
  312.     BUTTON,                    /* question type */
  313.     0,                        /* tile handle */
  314.     NULL, NULL, NULL,        /* in, out, and action */
  315.     " ~Abort ",                /* statement */
  316.     27, 7,                    /* statement x, y */
  317.     XALTA,                    /* shortcut key */
  318.     box1,                    /* border characters */
  319.     HEAD_ON,                /* shading */
  320.     XESC,                    /* hitting button is = XESC */
  321.     XESC,                    /* flash key */
  322.     FALSE,                    /* continuous */
  323.     ABORTED                    /* exit value */
  324. };
  325.  
  326. /*
  327.  * radio-check buttons, if assigned to a radio group then only
  328.  * one member of the group can be ON at one time.  Also, effects
  329.  * the way items are navigated to.  For example, when navigating
  330.  * from any item to an item within a radio group dbrun will select
  331.  * the currently selected item within the radio group, rather than
  332.  * the next item in the question list.  Also, navigating onto a
  333.  * radio group item will automatically check it.  to navigate between
  334.  * items within a radio button group use the LEFT and RIGHT commands
  335. */
  336.  
  337. rcbutton check1 = {
  338.     RCBUTTON,                /* question type */
  339.     0,                        /* tile handle */
  340.     NULL, NULL, NULL,        /* in, out, and action */
  341.     "~Scaly patches",        /* statement */
  342.     2, 11,                    /* statement x, y */
  343.     XALTS,                    /* shortcut key */
  344.     CHECK,                    /* checkbox! */
  345.     FALSE,                    /* checked? */
  346.     NULL                    /* target variable */
  347. };
  348.  
  349. rcbutton check2 = {
  350.     RCBUTTON,                /* question type */
  351.     0,                        /* tile handle */
  352.     NULL, NULL, NULL,        /* in, out, and action */
  353.     "~Peeling skin",        /* statement */
  354.     22, 11,                    /* statement x, y */
  355.     XALTP,                    /* shortcut key */
  356.     CHECK,                    /* checkbox! */
  357.     FALSE,                    /* checked? */
  358.     NULL                    /* target variable */
  359. };
  360.  
  361. rcbutton radio1 = {
  362.     RCBUTTON,                /* question type */
  363.     0,                        /* tile handle */
  364.     NULL, NULL, NULL,        /* in, out, and action */
  365.     "Has ~fever",            /* statement */
  366.     2, 13,                    /* statement x, y */
  367.     XALTF,                    /* shortcut key */
  368.     RADIO(0),                /* radio button! */
  369.     FALSE,                    /* checked? */
  370.     NULL                    /* target variable */
  371. };
  372.  
  373. rcbutton radio2 = {
  374.     RCBUTTON,                /* question type */
  375.     0,                        /* tile handle */
  376.     NULL, NULL, NULL,        /* in, out, and action */
  377.     "No F~ever",            /* statement */
  378.     22, 13,                    /* statement x, y */
  379.     XALTE,                    /* shortcut key */
  380.     RADIO(0),                /* radio button! */
  381.     TRUE,                    /* checked? */
  382.     NULL                    /* target variable */
  383. };
  384.  
  385. /* various bits of data for use in next question... */
  386. char *alistchoices[] = {
  387.     "apples",
  388.     "oranges",
  389.     "bananas",
  390.     "grapes",
  391.     "pears",
  392.     "kumquats",
  393.     "pomegranites",
  394.     "potatoes",
  395.     "carrots",
  396.     "peas",
  397.     "beets",
  398.     "wheat",
  399.     "oats",
  400.     "bran",
  401.     "corn",
  402.     "chicken",
  403.     "turkey",
  404.     "beef",
  405.     "pork",
  406.     "ham",
  407.     "bacon",
  408.     NULL
  409. };
  410. char lotsofones[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  411. char lotsofzeros[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  412.  
  413. /* this is a list box question */
  414. list alist = {
  415.     LIST,                    /* question type -- must be LIST */
  416.     0,                        /* tile to display list box in */
  417.     NULL,NULL,NULL,            /* edit checking routines */
  418.     "Test",                    /* statement */
  419.     2,14,                    /* statement coordinates */
  420.     XALTT,                    /* shortcut key */
  421.     SELECT_ONE,                /* list type -- must be SELECT_ONE or CHECKLIST */
  422.     MAX_WINDOWS,            /* the list box uses a window to display itself in */
  423.     4,40,                    /* if the above window handle is not valid we use */
  424.     3,15,                    /* this info to create the window */
  425.     1,1,
  426.     4,40,
  427.     HEAD_ON,
  428.     NULL,
  429.     NULL,
  430.     box1,
  431.     NO_BARS,
  432.  
  433.     0,                        /* initially selected element in the list */
  434.     alistchoices,            /* null terminated list of elements in the list */
  435.     lotsofones,                /* a list of chars corresponding to the list of
  436.                                elements where 0 = not available, 1 = available,
  437.                                and 2 = title. */
  438.     lotsofzeros,            /* a list of chars corresponding to the list of
  439.                                elements where a 0 = toggled on and 1 = toggled
  440.                                off */
  441.     " "," ",                /* on and off strings */
  442.     NULL,                    /* a list question requires a list_rec structure.
  443.                                set this to NULL to have db_run create the
  444.                                structure as needed.  If you wish to supply
  445.                                the structure set this to point to the
  446.                                structure */
  447.     NULL,                    /* target variable */
  448.     NULL,                    /* macros */
  449.     TRUE,                    /* when TRUE list is redrawn.  Set to TRUE when
  450.                                initializing structure */
  451.     FALSE,                  /* autodescend.  Set this to FALSE so that cursor
  452.                                will remain in border of list when the list box
  453.                                is selected.  Once user enters list this value
  454.                                is set to TRUE.  Using FALSE when you initialize
  455.                                the str structure will make it work like MAC &
  456.                                MS-WINDOWS. */
  457.     255                        /* maxcols.  Set this value to the maximum number
  458.                                of columns you want displayed side by side in
  459.                                the list.  db_run will attempt to fill the
  460.                                columns rowwise creating additional columns as
  461.                                needed until this number is reached.  Once this
  462.                                number is reached it adds additional rows to
  463.                                the virtual screen.
  464.                             */
  465. };
  466.  
  467.  
  468. void *simple_questions[] = {
  469.     &first_question,
  470.     &second_question,
  471.     &confirm_button,
  472.     &abort_button,
  473.     &check1,
  474.     &check2,
  475.     &radio1,
  476.     &radio2,
  477.     &alist,
  478.     NULL
  479. };
  480.  
  481. dialog_box simple_db = {
  482.  
  483.     MAX_WINDOWS,            /*     window handle -- set this to MAX_WINDOWS
  484.                                 if you want db_run to create the window
  485.                                 for you
  486.                             */
  487.     NULL, NULL,                /*     post-size and post-move routines -- after
  488.                                 sizing or moving a dialog box these
  489.                                 routines will be executed
  490.                             */
  491.     TRUE, FALSE,            /*     move and size OK -- set these to TRUE if
  492.                                 it is OK to move or size the dialog box
  493.                             */
  494.     simple_questions,        /*     questions -- set this to the array of
  495.                                 questions which comprise the dialog box
  496.                             */
  497.     db_available,            /*     available questions -- this is an array of 1s
  498.                                 an 0s where 1 indicates that a user can modify
  499.                                 the response to the question and 0 indicates
  500.                                 that it is not allowed.
  501.                             */
  502.     0,                        /*     selected question -- indicates the currently */
  503.     default_db_cmds,        /*     keyboard commands -- an array of ints which
  504.                                 assigns each db opcode to a keypress, e.g.
  505.                                 you might assign XENTER to CONFIRM and XESC
  506.                                 to ABORT.
  507.                             */
  508.     FALSE,                    /*     If FALSE the dialog box will be automatically
  509.                                 brought to the top of the window stack when
  510.                                 activated.  Otherwise, you will be able to
  511.                                 run the dialog box but other windows may
  512.                                 obscure it.  Normally set to TRUE for menu
  513.                                 bars, or some sort of dialog box which
  514.                                 represents a desktop.  For example you
  515.                                 could represent the Framework II desktop
  516.                                 as a background dialog box.
  517.                             */
  518.     &msmono,                /*     db_colors -- an array of unsigned chars
  519.                                 containing video attributes to use for
  520.                                 various parts of a dialog box
  521.                             */
  522.     &default_db_delimiters,    /*     db delimiters -- an array of chars which
  523.                                 are used to delimit the response areas
  524.                                 for various question types
  525.                             */
  526.     NULL,                    /*     insurance -- normally set to NULL, this
  527.                                 pointer is used internally by db_run.
  528.                                 When a dialog box is activated a copy
  529.                                 of the current responses is made and
  530.                                 pointed to by this field.  If the
  531.                                 user ABORTS the dialog box we restore
  532.                                 the original responses using this
  533.                                 copy.
  534.                             */
  535.     allmacros,                /*     macros -- indicates any special macros for
  536.                                 this dialog box.
  537.                             */
  538.  
  539.                             /*     All of these are used to create a
  540.                                 window to display the dialog box in
  541.                                 if the above window handle does not
  542.                                 exist.
  543.                             */
  544.     22, 50,                    /* virtual rows and columns */
  545.     1, 1,                    /* physical x & y */
  546.     1, 1,                    /* virtual x & y */
  547.     22, 50,                    /* viewport rows & columns */
  548.     HEAD_ON,                /* shading style */
  549.     "A simple dialog box",    /* primary dialog box name */
  550.     "secondary name",        /* secondary dialog box name */
  551.     box1,                    /* window border characters */
  552.     NONE,                    /* scroll bars style */
  553.     lightgray, black        /* the virtual screen attributes */
  554. };
  555.  
  556. void main()
  557. {
  558.     int result;
  559.     unsigned char head = 0, tail = 0;
  560.     unsigned int queue[256];
  561.  
  562.     /* initialize The WINDOW PRO - required */
  563.     wn_init();
  564.  
  565.     /* call blip during the keyboard/mouse polling loop */
  566.     idle = blip;
  567.  
  568.     /* initialize The DB PRO message queue - required */
  569.     db_switchqueue(&head, &tail, queue);
  570.  
  571.     /* execute the dialog box */
  572.     result = db_run(&simple_db, 0);
  573.  
  574.     /* get rid of the dialog box - if memory is tight you can deleted
  575.        the dialog box window each time. Doing this will slow down
  576.        execution but allow you to cram in more data.  Note that
  577.        if you delete a dialog box window you should reset the
  578.        window handle back to MAX_WINDOWS.  When a window handle
  579.        is deleted it is returned to the free handles pool.  If the
  580.        handle were to get reused and this dialog box were rerun
  581.        it would believe that it had a valid window handle and
  582.        would not create a new window (of the proper size and location.)
  583.     */
  584.     wn_delw(simple_db.handle); simple_db.handle = MAX_WINDOWS;
  585.  
  586.     /* restore the cursor position */
  587.     v_gotoxy(oldx,oldy);
  588.  
  589.     /* restore the cursor shape */
  590.     v_curshape(oldb, olde);
  591.  
  592.     /*
  593.        display the results -- demonstrates how running a dialog box
  594.        updates application variables.
  595.     */
  596.     printf("The user exited the dialog box by %s.\n",exit_codes[result - 1]);
  597.     printf("Occupation = %s\n",responses[job]);
  598.     printf("Name = %s\n",name);
  599.  
  600. }
  601.  
  602. /* this is a sample polling loop function */
  603. int blip(v)
  604. void *v;
  605. {
  606.     static int pos = 1;
  607.  
  608.     vs_putc(simple_db.handle,0,pos,21,white,black,' ');
  609.     pos++;
  610.     if (pos > 50) pos = 1;
  611.     vs_putc(simple_db.handle,0,pos,21,white,black,'>');
  612.     return(1);
  613. }
  614.